<template>
  <div>
    <div class="qrcode"
         ref="qrCodeUrl"> </div>
  </div>

</template>

<script>
import QRCode from 'qrcodejs2'
import vueQr from 'vue-qr'
import axios from 'axios'
import { initDynamicRoutes } from '@/router'
export default {
  data () {
    return {
      key: '',
      url: '',
      timer: '',
      uid: '',
      usernmae: ''
    }
  },
  components: {
    vueQr
  },
  methods: {
    /**
     * 得到key
     */
    getKey () {
      axios({
        url: 'http://43.138.186.111:8082/sgudp/Qr/getkey'
      }).then(re => {
        this.key = re.data.msg

        this.getUrl()
      })
    },
    /**
     * 得到url
     */
    getUrl (key) {
      console.log(this.key + '10883')
      axios({
        url: 'http://43.138.186.111:8082/sgudp/Qr/getUrl?key=',
        params: {
          key: this.key
        }
      }).then(re => {
        this.url = re.data.data.url
        this.creatQrCode()
      })
    },
    start () {
      this.timer = setInterval(this.valChange, 5000); // 注意: 第一个参数为方法名的时候不要加括号;
    },
    valChange () {
      console.log(111)
      this.check()
    },
    /**
 * 生成2微码
 */
    creatQrCode () {
      console.log('考考考考考考考试' + this.key)

      var qrcode = new QRCode(this.$refs.qrCodeUrl, {
        text: "http://43.138.186.111:8082/sgudp/Qr/getUrl?key=" + this.key, // 需要转换为二维码的内容
        width: 100,
        height: 100,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },

    /**
     * 检查登录
     */
    check () {
      console.log('dingshi')
      console.log(this.key)
      axios({
        url: 'http://43.138.186.111:8082/sgudp/Qr/check',
        params: {
          key: this.key
        }
      }).then(res => {

        console.log('登录....................')
        console.log(res.headers.authorization)
        console.log(res.data.data)
        // 登录成功跳转
        // localStorage.setItem('currentUser_name', res.data.data.userName)
        // localStorage.setItem('Authorization', res.headers.authorization)
        // this.$router.push({ path: '/home' })
    



        console.log('路由....................................................')

        // 将用户信息存入vuex,
        this.$store.commit('setRole', res.data.data.role)
        this.$store.commit('setUsername', res.data.data.username)
        sessionStorage.setItem('token', res.data.data.role + '-token')
        // 缺少了token
        localStorage.setItem('currentUser_name', res.data.data.username)
        localStorage.setItem('Authorization', res.headers.authorization)
        // 根据用户所具备的权限 动态添加路由规则

        if (sessionStorage.getItem('role') === '盘查员') {
          const data = [
            {
              id: 1,
              authName: '仓库管理',
              children: [
                {
                  id: 11,
                  authName: '盘查处理',
                  path: 'chuli',
                  rights: ['view', 'edit', 'add', 'delete']
                }
              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '经理') {
          const data = [
            {
              id: 1,
              authName: '物流管理',

              children: [{
                id: 11,
                authName: '入库查看',
                path: 'totalCheck',
                rights: ['view']
              },
              {
                id: 12,
                authName: '出库查看',
                path: 'outputCheck',

                rights: ['view']
              }

              ]
            },
            {
              id: 2,
              authName: '货物管理',
              children: [{
                id: 21,
                authName: '查看',
                path: 'check',

                rights: ['view']
              }]
            },
            {
              id: 3,
              authName: '仓库管理',
              children: [{
                id: 31,
                authName: '盘查查看',
                path: 'chakan',
                rights: ['view']
              }]
            },
            {
              id: 4,
              authName: '员工管理',
              children: [{
                id: 41,
                authName: '处理',
                path: 'handle2',
                rights: ['view', 'edit', 'add', 'delete']
              }]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '技术管理员') {
          const data = [
            {
              id: 1,
              authName: '技术管理',
              children: [
                {
                  id: 11,
                  authName: '数据导出',
                  path: 'Backup',

                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 12,
                  authName: '数据导入',
                  path: 'Recover',

                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 13,
                  authName: '恢复默认',
                  path: 'Default',

                  rights: ['view', 'edit', 'add', 'delete']
                }
              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '货物管理员') {
          const data = [
            {
              id: 1,
              authName: '货物管理',

              children: [
                {
                  id: 11,
                  authName: '处理',
                  path: 'handle',

                  rights: ['view', 'edit', 'add', 'delete']

                }
              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '入库员') {
          const data = [
            {
              id: 1,
              authName: '物流管理',
              children: [
                {
                  id: 11,
                  authName: '入库员',
                  path: 'theFirstInput',
                  rights: ['view', 'edit', 'add', 'delete']
                }

              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '入库核验员') {
          const data = [
            {
              id: 1,
              authName: '物流管理',
              children: [
                {
                  id: 11,
                  authName: '入库核验员',
                  path: 'totalCheck',
                  rights: ['view', 'edit', 'add', 'delete']
                }
              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '出库员') {
          const data = [
            {
              id: 1,
              authName: '物流管理',

              children: [{
                id: 11,
                authName: '出库员',
                path: 'output',

                rights: ['view', 'edit', 'add', 'delete']

              }]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '出库核验员') {
          const data = [

            {
              id: 1,
              authName: '物流管理',

              children: [{
                id: 11,
                authName: '出库核验',
                path: 'outputCheck',

                rights: ['view', 'edit', 'add', 'delete']
              }]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '环境管理员') {
          const data = [

            {
              id: 1,
              authName: '仓库管理',

              children: [
                {
                  id: 11,
                  authName: '仓库肉类',
                  path: 'meat',
                  show: true,
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 12,
                  authName: '仓库果蔬类',
                  path: 'fruit',
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 13,
                  authName: '仓库加工类',
                  path: 'process',

                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 14,
                  authName: '仓库蛋类',
                  path: 'egg',

                  rights: ['view', 'edit', 'add', 'delete']
                }

              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('dddd')
          this.$router.push('/home')
        }
        if (sessionStorage.getItem('role') === '超级管理员') {
          const data = [
            {
              id: 1,
              authName: '物流管理',
              children: [{
                id: 11,
                authName: '入库员',
                path: 'theFirstInput',
                rights: ['view', 'edit', 'add', 'delete']
              },
              {
                id: 12,
                authName: '入库核验员',
                path: 'totalCheck',
                rights: ['view', 'edit', 'add', 'delete']
              },
              {
                id: 13,
                authName: '出库员',
                path: 'output',
                rights: ['view', 'edit', 'add', 'delete']

              },
              {
                id: 14,
                authName: '出库核验员',
                path: 'outputCheck',
                rights: ['view', 'edit', 'add', 'delete']
              }
              ]
            },
            {
              id: 2,
              authName: '货物管理',
              children: [{
                id: 21,
                authName: '查看',
                path: 'check',
                rights: ['view']
              },
              {
                id: 22,
                authName: '处理',
                path: 'handle',
                rights: ['view', 'edit', 'add', 'delete']
              }]
            },
            {
              id: 3,
              authName: '仓库管理',
              children: [
                {
                  id: 31,
                  authName: '仓库肉类',
                  path: 'meat',
                  show: true,
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 32,
                  authName: '仓库果蔬类',
                  path: 'fruit',
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 33,
                  authName: '仓库加工类',
                  path: 'process',
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 34,
                  authName: '仓库蛋类',
                  path: 'egg',
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 35,
                  authName: '盘查查看',
                  path: 'chakan',
                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 35,
                  authName: '盘查记录',
                  path: 'chuli',
                  rights: ['view', 'edit', 'add', 'delete']
                }
              ]
            },
            {
              id: 4,
              authName: '员工管理',
              children: [{
                id: 41,
                authName: '处理',
                path: 'handle2',
                rights: ['view', 'edit', 'add', 'delete']
              },
              {
                id: 42,
                authName: '查看',
                path: 'check2',
                rights: ['view', 'edit', 'add', 'delete']
              }
              ]
            },
            {
              id: 5,
              authName: '技术管理',
              children: [
                {
                  id: 51,
                  authName: '数据导出',
                  path: 'Backup',

                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 52,
                  authName: '数据导入',
                  path: 'Recover',

                  rights: ['view', 'edit', 'add', 'delete']
                },
                {
                  id: 53,
                  authName: '恢复默认',
                  path: 'Default',
                  rights: ['view', 'edit', 'add', 'delete']
                }
              ]
            }
          ]
          this.$store.commit('setRightList', data)
          console.log('eeeee')
          initDynamicRoutes()
          console.log('路由跳转.........................................................')
          this.$router.push('/home')
          clearInterval(this.timer)
          // 后端登录
          // axios({
          //   url: "'http://sgudp.vipgz4.91tunnel.com/scanLogin",
          //   params: {
          //     token: localStorage.getItem('Authorization', res.headers.authorization)
          //   }
          //  })
        }
      })
    }
  },
  mounted () {
    this.getKey()
    this.start()

  },
  beforeDestroy () {
    clearInterval(this.timer);
    this.timer = null;
  }
}
</script>

<style scoped>
.qrcode {
  display: inline-block;
  width: 132px;
  height: 132px;
  background-color: #fff;
  padding: 6px;
  box-sizing: border-box;
}
</style>
